package com.j2ee.cglib.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class PersonInterceptor implements MethodInterceptor {
	
	private Object target ;
	private Transaction transaction ;
	
	public PersonInterceptor(Object target, Transaction transaction) {
		super();
		this.target = target;
		this.transaction = transaction;
	}
	
	/**
	 * 创建代理对象
	 */
	public Object createProxy() {
		Enhancer enhancer = new Enhancer() ;
		enhancer.setCallback(this) ;
		//设置代理类的父类——目标类
		enhancer.setSuperclass(Person.class) ;
		return enhancer.create() ;
	}

	@Override
	public Object intercept(Object arg0, Method method, Object[] arg2, MethodProxy arg3) throws Throwable {
		this.transaction.beginTransaction() ;
		method.invoke(target, arg2) ;
		this.transaction.commit() ;
		return null;
	}
	
	

}
